import { getUserInfo, bindSystem } from "@/api/login";
import { setToken,removeToken } from "@/utils/auth"; // getToken from cookie

const user = {
  state: {

  },
  mutations: {

  },
  actions: {
    // 获取用户信息
    wxUserDataList({ commit }, params) {
      return new Promise((resolve, reject) => {
        getUserInfo(params)
          .then(res => {
              console.log('res: ', res);
            if ((res.data.status == 100)) {
              resolve(res.data.data);
            } else {
              reject(new Error("错误"));
            }
          })
          .catch(error => {
            reject(error);
          });
      });
    },
    // 点击登录
    weXinLogin({ commit }, params) {
      return new Promise((resolve, reject) => {
        bindSystem(params)
          .then(res => {
            if ((res.data.status == 100 && res.data.data.token)) {
              setToken({
                key: res.data.data.token_key,
                val: res.data.data.token
              });
              resolve(res.data.data);
            } else {
              reject(new Error("错误"));
            }
          })
          .catch(error => {
            reject(error);
          });
      });
    },
    // 前端 登出
    async FedLogOut({ commit }) {
      removeToken()
    },
  }
};

export default user;
